﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace AleScheduler
{
    public partial class FormHappyHour : Form
    {
        public FormHappyHour()
        {
            InitializeComponent();
        }

        private string _schname;
        public string schName
        {
            get
            {
                return _schname;
            }
            set
            {
                _schname = value;
            }
        }

        private void chkallday_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkallday.Checked)
            {
                this.dateTimePicker1.CustomFormat = "hh : mm";
                this.dateTimePicker2.CustomFormat = "hh : mm";
            }
            else
            {
                this.dateTimePicker1.CustomFormat = "d/M/yyyy";
                this.dateTimePicker2.CustomFormat = "dd/MM/yyyy - hh : mm";
            }

                this.dateTimePicker1.ShowUpDown = this.chkallday.Checked;
                this.dateTimePicker2.ShowUpDown = this.chkallday.Checked;
        }

        private void FormHappyHour_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.btnCancel.PerformClick();
        }

        private void FormHappyHour_Load(object sender, EventArgs e)
        {
            this.dateTimePicker2.MinDate = DateTime.Now.Date;
            this.chkallday.Checked = true;
            this.chkallday.Visible = false;

            RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey(this.schName, true);
            string spStart = (string)reg.GetValue("HStartPeriod", string.Empty);
            string spStop = (string)reg.GetValue("HStopPeriod", string.Empty);
            if (spStart != string.Empty && spStart.Length > 10)
            {
             //DateTime Complete for Future Impletation
            }
            else if (spStart != string.Empty && spStart.Length < 10)
            {
                this.chkallday.Checked = true;
                this.chkStart.Checked = true;
                System.Globalization.DateTimeFormatInfo dtfi = (System.Globalization.DateTimeFormatInfo)System.Globalization.DateTimeFormatInfo.CurrentInfo.Clone();
                dtfi.ShortTimePattern = "hh.mm";
                DateTime dt = DateTime.Parse(reg.GetValue("HStartPeriod").ToString(), dtfi);
                this.dateTimePicker1.Value = dt;
            }

            if (spStop != string.Empty && spStop.Length > 10)
            {
                //DateTime Complete for Future Impletation
            }
            else if (spStop != string.Empty && spStop.Length < 10)
            {
                this.chkallday.Checked = true;
                this.chkStop.Checked = true;
                System.Globalization.DateTimeFormatInfo dtfi = (System.Globalization.DateTimeFormatInfo)System.Globalization.DateTimeFormatInfo.CurrentInfo.Clone();
                dtfi.ShortTimePattern = "hh.mm";
                DateTime dt = DateTime.Parse(reg.GetValue("HStopPeriod").ToString(), dtfi);
                this.dateTimePicker2.Value = dt;
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            RegistryKey reg = Registry.LocalMachine.OpenSubKey("Software", true).OpenSubKey("AleScheduler", true).OpenSubKey(this.schName, true);
            string spStart = (string)reg.GetValue("HStartPeriod", string.Empty);
            string spStop = (string)reg.GetValue("HStopPeriod", string.Empty);
            //Time Formatted with :
            string hour1 = this.dateTimePicker1.Value.ToShortTimeString().Replace(".",":");
            string hour2 = this.dateTimePicker2.Value.ToShortTimeString().Replace(".",":");
            // Happy Hour Start 
            if (this.chkallday.Checked && this.chkStart.Checked)
            {
                reg.SetValue("HStartPeriod",hour1);
            }
            else if (this.chkStart.Checked)
            {
                //Save Happy Hour Start
                reg.SetValue("HStartPeriod", this.dateTimePicker1.Value.ToShortDateString() + " - " + hour1);
            }
            else
            {
                if (spStart != string.Empty)
                    reg.DeleteValue("HStartPeriod");
            }
            //Happy Hour Stop
            if (this.chkallday.Checked && this.chkStop.Checked)
            {
                reg.SetValue("HStopPeriod", hour2);
            }
            else if (this.chkStop.Checked)
            {
                reg.SetValue("HStopPeriod", this.dateTimePicker2.Value.ToShortDateString() + " - " + hour2);
            }
            else
            {
                if (spStop != string.Empty)
                    reg.DeleteValue("HStopPeriod");
            }
            reg.Close(); 
        }
    }
}
